package com.zdxlz.fcmp.upms.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdxlz.fcmp.common.core.constant.enums.OperateTypeEnum;
import com.zdxlz.fcmp.common.core.util.R;
import com.zdxlz.fcmp.common.log.annotation.LogRecord;
import com.zdxlz.fcmp.common.security.util.UserLoginInfoUtil;
import com.zdxlz.fcmp.upms.api.dto.SystemUserDto;
import com.zdxlz.fcmp.upms.api.dto.SystemUserReqDto;
import com.zdxlz.fcmp.upms.api.entity.SystemUser;
import com.zdxlz.fcmp.upms.api.service.ISystemUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import static com.zdxlz.fcmp.common.core.constant.CommonConstants.*;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author: aman
 * @since 2025-01-30
 */
@LogRecord(module = "用户管理模块")
@Tag(name = "用户管理模块")
@RestController
@RequestMapping("/api/v1/sys/user")
@AllArgsConstructor
public class SystemUserController {
    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();

    private ISystemUserService systemUserService;

    @GetMapping
    @Operation(summary = "分页查询用户")
    @LogRecord(module = "分页查询用户", value = "查询条件，租户ID【{}】，用户手机号【{}】，用户名称【{}】", field = {"tenantId", "mobile", "username"}, operType = OperateTypeEnum.SELECT)
    public R queryUserList(@ParameterObject Page<SystemUser> page, @ParameterObject SystemUserReqDto dto) {
        return R.ok(systemUserService.queryUserList(page, dto), OPERATION_SUCCESS);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询单个用户")
    @LogRecord(module = "查询单个用户", value = "查询条件，用户ID【{}】", field = {"id"}, operType = OperateTypeEnum.SELECT)
    public R getUserById(@PathVariable Long id) {
        return R.ok(systemUserService.getById(id), OPERATION_SUCCESS);
    }

    @PostMapping
    @Operation(summary = "新增用户")
    @LogRecord(module = "新增用户", value = "新增用户必要条件，用户名称：【{}】，用户手机号：【{}】", db = true, field = {"username", "mobile"}, operType = OperateTypeEnum.INSERT)
    public R insertUser(@RequestBody @Validated SystemUserDto dto) {
        dto.setPassword(ENCODER.encode(DEFAULT_PASSWORD));
        dto.setTenantId(UserLoginInfoUtil.getUserInfo().getTenantId());
        return R.ok(systemUserService.insertUser(dto), CREATE_SUCCESS);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新用户")
    @LogRecord(module = "更新用户", value = "更新用户必要条件，用户ID【{}】，用户名称：【{}】，用户手机号：【{}】", db = true, field = {"id", "username", "mobile"}, operType = OperateTypeEnum.UPDATE)
    public R updateUser(@PathVariable Integer id, @RequestBody SystemUserDto dto) {
        dto.setId(id);
        return R.ok(systemUserService.updateUser(dto), UPDATE_SUCCESS);
    }

    @DeleteMapping
    @Operation(summary = "删除用户")
    @LogRecord(module = "删除用户", value = "删除用户必要条件，用户ID【{}】", db = true, field = {"ids"}, operType = OperateTypeEnum.DELETE)
    public R deleteUserById(@RequestBody List<Integer> ids) {
        return R.ok(systemUserService.deleteUser(ids), DELETE_SUCCESS);
    }

}
